package com.intelli.eve.core.impl.configuration;

import com.intelli.ray.core.ManagedComponent;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Author: Sergey42
 * Date: 21.12.2014 17:20
 */
@ManagedComponent(name = "Configuration")
public class Configuration {

    protected Map<String, String> properties;

    public Configuration() {
        properties = new ConcurrentHashMap<>(64);
        ConfigurationLoader.loadProperties("/configuration.properties", properties);
    }

    public String getProperty(String key) {
        return properties.get(key);
    }

    public int getIntProperty(String key, int defaultValue) {
        String property = getProperty(key);
        try {
            return Integer.parseInt(property);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }

    public void setProperty(String key, String value) {
        properties.put(key, value);
    }

    public static class ConfigurationInitializationException extends RuntimeException {
        public ConfigurationInitializationException(Throwable e) {
            super(e);
        }
    }
}
